﻿var quest = {
    url: "/Qn/QnPaper/SubmitForm",
    paperId: undefined,
    questionCount: undefined,
    candidateJob: undefined,
    answers: undefined,
    initPage: function (paperId, answers) {
        if (answers != undefined && answers != "") {
            quest.answers = answers;
            quest.initAnswers();
            return;
        }
        quest.paperId = paperId;
        quest.questionCount = $(".question").length;
        $(".survey_container input").each(function () {
            $(this).click(function () {
                $(this).parents(".question").find(".tips_error")
                    .html("")
                    .removeClass("tips_error")
                    .addClass("tips");
            });
        });
        $(".survey_submit").click(function () {
            if (!quest.checkGetInput()) {
                return;
            }
            $.ajax({
                type: 'POST',
                url: quest.url,
                data: {
                    openId: appCommon.getOpenId(),
                    paperId: quest.paperId,
                    answers: quest.answers
                },
                success: function (rst) {
                    //写入cookie已答卷的标记
                    $.cookie('qnId', quest.paperId, { domain: ".smartyunfeng.top", path: "/" });
                    if (rst.state == "success") {
                        $(".survey_suffix").show();
                        $(".survey_suffix").siblings().hide();
                    } else {
                        alert(rst.message);
                        window.location = "";
                    }
                },
                dataType: "json"
            });
        });
    },
    initAnswers: function () {
        var qas = quest.answers.split(";");
        for (var i = 0; i < qas.length; i++) {
            var qa = qas[i].split(":");
            var qid = qa[0];
            if ($("#question_" + (i + 1)).attr("data-type") == "textarea") {
                $("#answer_" + (i + 1)).val(qa[1]);
                continue;
            }
            var anss = qa[1].split(",");
            for (var j = 0; j < anss.length; j++) {
                $("#option_" + qid + "_" + anss[j]).attr("checked", "true");
            }
        }
        $(".survey_container input").attr("disabled", "true");
        $(".survey_container textarea").attr("disabled", "true");
        $(".survey_submit").hide();
        $(".survey_assess_result").show();
    },
    checkGetInput: function () {
        var qas = [];
        $(".question").each(function () {
            var dataId = $(this).attr("data-id");
            var dataType = $(this).attr("data-type");
            if (dataType == "textarea") {
                var tav = $("#answer_" + dataId).val();
                tav = tav.replace(":", "：").replace(";", "；");
                tav = encodeURI(tav);
                qas.push(dataId + ":" + tav);
            } else {
                var chkVallue = quest.getChkValue("answer_" + dataId);
                if (chkVallue.length == 0) {
                    quest.scrollOffset($("#question_" + dataId).offset());
                    $(this).find(".tips")
                        .html("这道题必须回答哦")
                        .removeClass("tips")
                        .addClass("tips_error");
                    return false;
                }
                var vs = chkVallue.join(",");
                qas.push(dataId + ":" + vs);
            }
        });
        quest.answers = qas.join(";");
        return true;
    },
    getChkValue: function (name) {
        var chkValue = [];
        $('input[name="' + name + '"]:checked').each(function () {
            chkValue.push($(this).val());
        });
        return chkValue;
    },
    scrollOffset: function (scrolloffset) {
        $("body,html").animate({
            scrollTop: scrolloffset.top - 70
        }, 0);
    },
    tip:function(options) {
        
    }
}